var dbConfig = require('../util/dbconfig')
var moment = require('moment');
// 获取chart表
// sql语句
getAll = (req, res) => {
	//查表所有数据
	var sql = 'select * from express';
	//传参数
	var sqlArr = [];
	//回调
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'data': data,
				'status':true
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
query = (req, res) => {
	//查表所有数据
	var sql = 'select * from express';
	//传参数
	var sqlArr = [];
	//回调
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'data': data,
				'status':true
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//获取指定分类的文章
getCateidAll = (req, res) => {
	console.log(req)
	console.log(req.body)
	console.log(req.query)
	let {
		id
	} = req.body;
	//查表cate_id=xx 的数据
	var sql = 'select * from post where cate_id=?';
	var sqlArr = [id];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'list': data
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//增加
getAdd = (req, res) => {
	console.log(req)
	let title = req.body.title;
	let author = req.body.author;
	let pageviews = req.body.pageviews;
	let status = req.body.status;
	let time = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
	console.log(time)
	var sql = 'insert into chart (title,author,pageviews,status,time) values(?,?,?,?,?)';
	var sqlArr = [title, author, pageviews, status, time];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'msg': '成功'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//删(硬删除)
getdel = (req, res) => {
	console.log(req)
	let id = req.query.id;
	var sql = 'delete from chart where id = ?';
	var sqlArr = [id];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'msg': '指定id删除成功'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//删(软删除=改)
getdelStatus = (req, res) => {
	console.log(req)
	let id = req.query.id;
	let status = req.query.status;
	var sql = 'update chart set status = ? where id = ? ';
	var sqlArr = [status, id];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'msg': '指定id软删除成功'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//改
getupdate = (req, res) => {
	console.log(req)
	let id = req.body.id;
	let title = req.body.title;
	let author = req.body.author;
	let pageviews = req.body.pageviews;
	let status = req.body.status;
	let time = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
	console.log(time)
	var sql = 'update chart set title =?,author =?,pageviews =?,status =?,time = ? where id = ? ';
	var sqlArr = [title, author, pageviews, status, time, id];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'msg': '指定id修改其他数据成功'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

module.exports = {
	getAll,
	getCateidAll,
	getAdd,
	getdel,
	getdelStatus,
	getupdate,
	query
}
